package sk.stuba.fiit.team14.game;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.anddev.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.anddev.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import sk.stuba.fiit.team14.game.ai.DummyAI;
import sk.stuba.fiit.team14.game.mapengine.*;

public class SinglePlayerGameActivity extends BaseGameActivity {

	private static final int CAMERA_WIDTH = 480;
    private static final int CAMERA_HEIGHT = 320;
    
    private Camera mCamera;
    
	private BitmapTextureAtlas mOnScreenControlTexture;
	private TextureRegion mOnScreenControlBaseTextureRegion;
	private TextureRegion mOnScreenControlKnobTextureRegion;
	
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TextureRegion mPlayerTextureRegion;
	private TextureRegion mCrateTextureRegion;
	private TextureRegion mWallTextureRegion;

	private DigitalOnScreenControl mDigitalOnScreenControl;
	
	private	Map mGameMap;
	private Player mPlayer;
	private Player mPlayer2;
	private DummyAI dummyAI;
	
	@Override
	public Engine onLoadEngine() {
		mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
	    return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		mOnScreenControlTexture = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
		mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
		
		/* Load all the textures this game needs. */
		mBitmapTextureAtlas = new BitmapTextureAtlas(128, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
		mCrateTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "box.png", 32, 0);
		mWallTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "wall red.png", 64, 0);
		
		this.mEngine.getTextureManager().loadTextures(mBitmapTextureAtlas, mOnScreenControlTexture);
	}

	@Override
	public Scene onLoadScene() {
		mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new ColorBackground(0, 0, 0.8784f));
		
		mGameMap = new Map();
		mPlayer = new Player(0, 1, 32, 32, mPlayerTextureRegion);
		mPlayer2 = new Player(14, 0, 32, 32, mPlayerTextureRegion);
		dummyAI = new DummyAI(mPlayer2);
		
		mGameMap.addComponent(mPlayer);
		mGameMap.addComponent(mPlayer2);
		
		scene.attachChild(mPlayer);
		scene.attachChild(mPlayer2);
		//
		// TODO nacitat z nejakeho rozumneho suboru
		// alebo spravit mGameMap.randomGenerateLevel();
		
		for (int i=0; i < 8; i++) {
			Wall wall = new Wall(2, i, 32, 32, mWallTextureRegion);
			mGameMap.addComponent(wall);
			scene.attachChild(wall);
		}
		
		for (int i=8; i < 10; i++) {
			Crate crate = new Crate(2,i, 32, 32, mCrateTextureRegion);
			mGameMap.addComponent(crate);
			scene.attachChild(crate);
		}
		
		for (int i=4; i < 7; i++) {
			Crate crate = new Crate(3,i, 32, 32, mCrateTextureRegion);
			mGameMap.addComponent(crate);
			scene.attachChild(crate);
		}
		
		for (int i=0; i < 8; i++) {
			Wall wall = new Wall(5, 9-i, 32, 32, mWallTextureRegion);
			mGameMap.addComponent(wall);
			scene.attachChild(wall);
		}
		
		for (int i=0; i < 8; i++) {
			Crate crate = new Crate(6, 9-i, 32, 32, mCrateTextureRegion);
			mGameMap.addComponent(crate);
			scene.attachChild(crate);
		}
		
		for (int i=0; i < 8; i++) {
			Wall wall = new Wall(10, i, 32, 32, mWallTextureRegion);
			mGameMap.addComponent(wall);
			scene.attachChild(wall);
		}
		
		for (int i=0; i < 4; i++) {
			Crate crate = new Crate(11+i, 9-i, 32, 32, mCrateTextureRegion);
			mGameMap.addComponent(crate);
			scene.attachChild(crate);
		}
		
		for (int i=0; i < 4; i++) {
			Crate crate = new Crate(14-i, 4-i, 32, 32, mCrateTextureRegion);
			mGameMap.addComponent(crate);
			scene.attachChild(crate);
		}
		
		/* The On-Screen Controls to control the direction of the snake. */
		mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - mOnScreenControlBaseTextureRegion.getHeight(), mCamera, mOnScreenControlBaseTextureRegion, mOnScreenControlKnobTextureRegion, 0.1f, new IOnScreenControlListener() {
			@Override
			public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
				if(pValueX == 1) {
					SinglePlayerGameActivity.this.mPlayer.move(1);
				} else if(pValueX == -1) {
					SinglePlayerGameActivity.this.mPlayer.move(3);
				} else if(pValueY == 1) {
					SinglePlayerGameActivity.this.mPlayer.move(2);
				} else if(pValueY == -1) {
					SinglePlayerGameActivity.this.mPlayer.move(4);
				}
				SinglePlayerGameActivity.this.handleGame();
			}
		});
		
		this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);

		scene.setChildScene(mDigitalOnScreenControl);
		
		
		/* Make the Snake move every 0.5 seconds. */
		scene.registerUpdateHandler(new TimerHandler(0.5f, true, new ITimerCallback() {

			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				SinglePlayerGameActivity.this.updateAI();
			}
		}));
		
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}
	
	private void handleGame()
	{
		mGameMap.updateMap();
	}
	
	private void updateAI()
	{
		dummyAI.UpdateAI();
	}
	
}